﻿<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="../helpproject.xsl" ?>
<topic template="Default" lasteditedby="Geert" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../helpproject.xsd">
  <title translate="true">Unit testing in Windows Phone 7</title>
  <keywords>
    <keyword translate="true">Unit testing</keyword>
    <keyword translate="true">Windows Phone 7</keyword>
    <keyword translate="true">Windows Phone 7 Mango</keyword>
  </keywords>
  <body>
    <header>
      <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Unit testing in Windows Phone 7</text></para>
    </header>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Setting up unit tests for Windows Phone 7 isn’t as easy as you would expect. There is no unit test framework available out of the box, nor are there templates that allow you to create a unit test project. Some people are trying to outsmart the system by running regular Silverlight unit tests. I think that is a very bad idea because then you won’t test the logic against the WP7 framework, but against the Silverlight framework which is different.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Downloading the right libraries</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">First of all, there are special libraries needed to enable unit testing for WP7. The ones I use are in the </text><link displaytype="text" defaultstyle="true" type="weblink" href="http://silverlight.codeplex.com/" target="_blank" styleclass="Normal" translate="true">Windows Phone 7 toolkit</link><text styleclass="Normal" translate="true">. The following two libraries are needed:</text></para>
    <list id="1" type="ul" listtype="bullet" formatstring="&#183;" format-charset="SYMBOL_CHARSET" levelreset="true" legalstyle="false" startfrom="1" styleclass="Normal" style="font-family:Symbol; font-size:10pt; color:#000000;">
      <li styleclass="Normal"><text styleclass="Normal" translate="true">Microsoft.Silverlight.Testing.dll</text></li>
      <li styleclass="Normal"><text styleclass="Normal" translate="true">Microsoft.VisualStudio.QualityTools.UnitTesting.Silverlight.dll</text></li>
    </list>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Creating the unit test project</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Now we have the required libraries, let’s go and create the unit test project. The first step is to create a new Windows Phone 7 application:</text></para>
    <para style="text-align:justify; margin-top:16px; page-break-inside:avoid;"><image src="unittesting_wp7_createproject.png" scale="79.24%" width="757" height="523" styleclass="Image Caption"></image></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">After this step, make sure to add references to both your original Windows Phone 7 application and the test libraries you downloaded earlier in this chapter. Visual Studio might warn you that it might be unsafe to reference Silverlight assemblies, but you can ignore that.</text></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Modifying the MainPage</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Last thing to do is to modify the </text><text styleclass="Normal" style="font-style:italic;" translate="true">MainPage.xaml.cs </text><text styleclass="Normal" translate="true">which is automatically by the project template. Make sure the constructor of the </text><text styleclass="Normal" style="font-style:italic;" translate="true">MainPage</text><text styleclass="Normal" translate="true"> looks like the code below:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> MainPage()</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;InitializeComponent();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;Content = UnitTestSystem.CreateTestPage();</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">You are now ready to create your first unit test! Let’s do that to make sure your unit tests are able to run. Add a new class called </text><text styleclass="Normal" style="font-style:italic;" translate="true">DemoTest</text><text styleclass="Normal" translate="true"> and use the following content:</text></para>
    <para styleclass="Code Example"><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">using</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> Microsoft.VisualStudio.TestTools.UnitTesting;</text><br/><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">[TestClass]</text><br/><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">class</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> DemoTest</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;[TestMethod]</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">public</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">&#32;</text><text styleclass="Code Example" style="font-weight:bold; font-style:normal; color:#000080;" translate="true">void</text><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> MyFirstUnitTest()</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;{</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160; &#160; &#160;Assert.Inconclusive(&quot;We need to write our first unit test&quot;);</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true"> &#160; &#160;}</text><br/><text styleclass="Code Example" style="font-weight:normal; font-style:normal; color:#000000;" translate="true">}</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">Finally if you run the unit test project, you will see the following screens (which should be familiar if you are used to writing unit tests in Silverlight):</text></para>
    <para style="margin-top:16px; page-break-inside:avoid;"><image src="unittesting_wp7_runall.png" scale="80.00%" width="304" height="568" styleclass="Image Caption"></image><text styleclass="Font Style" style="font-family:Calibri; font-size:11pt; font-weight:normal; font-style:normal; text-decoration:none; text-transform:none; vertical-align:baseline; color:#000000; background-color:transparent; letter-spacing:normal; letter-scaling:100%;" translate="true"> &#160; </text><image src="unittesting_wp7_resultallfailed.png" scale="80.00%" width="304" height="568" styleclass="Image Caption"></image></para>
    <para styleclass="Heading1"><text styleclass="Heading1" translate="true">Unit testing without mocking</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">In unit tests for WPF and Silverlight, you are probably used to mock all your interfaces. In Catel, we always provide a test implementation of every service so you are not forced to mock the services at all times. Of course it is still possible to mock services for WPF and Silverlight, but for Windows Phone 7 it’s a different kind of story.</text></para>
    <para styleclass="Normal"><text styleclass="Normal" translate="true">There are currently no mocking frameworks available for WP7 and there will probably never going to be mocking frameworks. You are probably thinking? Why not, there is a big market out there and I want to be able to use unit tests for my WP7 applications. Unfortunately, the </text><text styleclass="Normal" style="font-style:italic;" translate="true">Reflection.Emit</text><text styleclass="Normal" translate="true"> method is missing in WP7. This method is necessary to mock any objects.</text></para>
  </body>
</topic>
